package ui;

import core.engine.systemDBEngine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

// 定义一个函数式接口用于回调
@FunctionalInterface
interface LoadingCompleteCallback {
    void onLoadingComplete();
}

public class SplashScreen {
    private JWindow splashWindow;
    private JProgressBar progressBar;
    private Timer timer;
    private int progress = 0;
    private LoadingCompleteCallback callback;

    // 构造函数接收回调接口
    public SplashScreen(LoadingCompleteCallback callback) {
        this.callback = callback;
    }


    public void showSplash() {
        // 创建窗口
        splashWindow = new JWindow();
        splashWindow.setSize(600, 400);
        splashWindow.setLocationRelativeTo(null);

        // 设置窗口内容面板为自定义面板
        splashWindow.setContentPane(new JPanel(new BorderLayout()) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // 绘制渐变色背景
                Graphics2D g2d = (Graphics2D) g;
                GradientPaint gp = new GradientPaint(
                    0, 0, new Color(52, 152, 219),
                    getWidth(), getHeight(), new Color(41, 128, 185));
                g2d.setPaint(gp);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        });

        // 添加标题标签
        JLabel titleLabel = new JLabel("简易 DBMS 系统", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 28));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));

        // 添加描述标签
        JLabel descLabel = new JLabel("专业的数据库管理工具", JLabel.CENTER);
        descLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
        descLabel.setForeground(Color.WHITE);
        descLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 40, 0));

        // 添加图标（如果有图标资源）
        JLabel iconLabel = new JLabel();
        try {
            URL iconUrl = getClass().getResource("/resources/database-icon.png");
            if (iconUrl != null) {
                ImageIcon icon = new ImageIcon(iconUrl);
                // 调整图标大小
                Image image = icon.getImage().getScaledInstance(120, 120, Image.SCALE_SMOOTH);
                iconLabel.setIcon(new ImageIcon(image));
            }
        } catch (Exception e) {
            // 如果没有图标资源，使用默认图标
            iconLabel.setText("DBMS");
            iconLabel.setFont(new Font("Arial", Font.BOLD, 48));
            iconLabel.setForeground(Color.WHITE);
        }
        iconLabel.setHorizontalAlignment(JLabel.CENTER);
        iconLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));

        // 添加进度条
        progressBar = new JProgressBar();
        progressBar.setIndeterminate(false);
        progressBar.setStringPainted(true);
        progressBar.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
        progressBar.setForeground(new Color(46, 204, 113));
        progressBar.setBackground(new Color(236, 240, 241));
        progressBar.setBorder(BorderFactory.createEmptyBorder(10, 50, 30, 50));

        // 添加版本信息
        JLabel versionLabel = new JLabel("v 1.0.0", JLabel.RIGHT);
        versionLabel.setFont(new Font("Arial", Font.PLAIN, 12));
        versionLabel.setForeground(Color.WHITE);
        versionLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 20));

        // 布局组件
        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setOpaque(false);

        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.setOpaque(false);
        centerPanel.add(iconLabel, BorderLayout.CENTER);
        centerPanel.add(descLabel, BorderLayout.SOUTH);

        contentPanel.add(titleLabel, BorderLayout.NORTH);
        contentPanel.add(centerPanel, BorderLayout.CENTER);
        contentPanel.add(progressBar, BorderLayout.SOUTH);

        splashWindow.getContentPane().add(contentPanel, BorderLayout.CENTER);
        splashWindow.getContentPane().add(versionLabel, BorderLayout.SOUTH);

        // 显示窗口
        splashWindow.setVisible(true);

        // 模拟加载进度
        timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                progress += 1;

                // 在50%进度时检查并初始化系统表
                if (progress == 50) {
                    try {
                        progressBar.setString("初始化系统数据库...");
                        if (!systemDBEngine.checkSystemTablesInitialized()) {
                            systemDBEngine.initSystemTables();
                        }
                    } catch (Exception ex) {
                        progressBar.setString("系统初始化失败!");
                        timer.stop();
                        JOptionPane.showMessageDialog(splashWindow,
                            "系统初始化失败: " + ex.getMessage(),
                            "错误", JOptionPane.ERROR_MESSAGE);
                        System.exit(1);
                        return;
                    }
                }

                progressBar.setValue(progress);
                progressBar.setString("加载中 " + progress + "%");

                if (progress >= 100) {
                    timer.stop();
                    splashWindow.setVisible(false);
                    splashWindow.dispose();

                    // 触发回调通知主程序
                    if (callback != null) {
                        callback.onLoadingComplete();
                    }
                }
            }
        });
        timer.start();
    }


}